//
//  NSUserDefualManager.m
//  ShunTian
//
//  Created by zhanshu on 15/9/1.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "NSUserDefualManager.h"

@implementation NSUserDefualManager
+ (void)storageValue:(id)value forKey:(NSString *)key {
    [[NSUserDefaults standardUserDefaults] setObject:value forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
+ (id)getValueByKey:(NSString *)key {
    id value = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    return value;
}

#pragma mark - 搜索历史记录
+ (void)addNewRecord:(id)value forKey:(NSString*)key{
    NSMutableArray *marr = [[NSMutableArray alloc] init];
    NSArray *arr = [[self class] getValueByKey:key];
    [marr addObjectsFromArray:arr];
    [marr insertObject:value atIndex:0];
    
    for (int i = 1; i < marr.count; i ++) {
        if ([value isEqualToString:[marr objectAtIndex:i]]) {
            [marr removeObjectAtIndex:i];
        }
    }
    
    if ([key isEqualToString:SearchRecord]) {
        if (marr.count == 6) {
            [marr removeLastObject];
        }
    }
    
    [[self class] storageValue:marr forKey:key];
}
@end
